package com.company.cloud.uba.modules.mgr.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.redis.service.RedisUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.uba.modules.app.controller.req.UserShareAddParam;
import com.company.cloud.uba.modules.app.controller.req.UserShareQuery4PageParam;
import com.company.cloud.uba.modules.base.entity.UserShareInfo;
import com.company.cloud.uba.modules.base.service.UserShareInfoService;
import com.company.cloud.uba.modules.cache.models.UserShareModel;
import com.company.cloud.uba.modules.mgr.service.UserShareMgrService;
import com.company.cloud.uba.utils.UbaIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserShareMgrServiceImpl implements UserShareMgrService {


    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private UserShareInfoService userShareInfoService;


    @Override
    public UserShareInfo addUserShare(UserShareAddParam params,String referralCode, LoginSimpleUserInfo user, String anonymToken, String ip) {
        UserShareInfo entity=new UserShareInfo();
        entity.setShareCode(UbaIdUtils.getShareCode());
        if(user!=null){
            entity.setUserId(user.getId());
            entity.setToken(user.getToken());
        }
        entity.setAnonyToken(anonymToken);
        entity.setRelIdType(params.getRelIdType());
        entity.setRelId(params.getRelId());
        entity.setReferralCode(referralCode);
        entity.setShareWay(params.getShareWay());
        entity.setShareContent(params.getShareContent());
        entity.setClientType(params.getClientType());
        entity.setMobileNo(params.getClientDeviceNo());
        entity.setLlType(params.getLlType());
        entity.setLongitude(params.getLongitude());
        entity.setLatitude(params.getLatitude());
        if(user!=null){
            entity.setCman(user.getId());
            entity.setEman(user.getId());
        }else{
            entity.setCman(-1);
            entity.setEman(-1);
        }
        entity.setCdt(LocalDateTime.now());
        entity.setEdt(LocalDateTime.now());
        entity.setCip(ip);
        entity.setEip(ip);
        userShareInfoService.save(entity);
        return  entity;
    }

    @Override
    public IPage<UserShareModel> queryUserShare(UserShareQuery4PageParam params, LoginSimpleUserInfo user) {

        IPage<UserShareInfo> page = userShareInfoService.page(new Page<>(params.getPage(), params.getRows()),
                new QueryWrapper<UserShareInfo>()
                        .eq(StrUtil.isNotEmpty(params.getShareCode()),"share_code",params.getShareCode())
                        .eq(CheckUtils.hasInt(params.getUserId()),"user_id",params.getUserId())
                        .eq(StrUtil.isNotEmpty(params.getAnonymousToken()),"anonymous_token",params.getAnonymousToken())
                        .eq(StrUtil.isNotEmpty(params.getRelIdType()),"rel_id_type",params.getRelIdType())
                        .eq(StrUtil.isNotEmpty(params.getRelId()),"rel_id",params.getRelId())
                        .eq(StrUtil.isNotEmpty(params.getReferralCode()),"referral_code",params.getReferralCode())
                        .orderByDesc("cdt")
        );




//
        return null;
    }


}
